#include "hello_win.h"

LRESULT CALLBACK WndProc(HWND wnd, UINT message, WPARAM wparam, LPARAM lparam) {
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;

	switch (message) {
		case WM_CREATE:
			PlaySound(TEXT("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC);
			return 0;
		case WM_PAINT:
			hdc = BeginPaint(wnd, &ps);
			GetClientRect(wnd, &rect);
			DrawText(hdc, TEXT("Hello, windows 98!"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
			EndPaint(wnd, &ps);
			return 0;
		case WM_DESTROY:
			PostQuitMessage(0);
			return 0;
	}
	return DefWindowProc(wnd, message, wparam, lparam);
}

int HelloWin(HINSTANCE ins, HINSTANCE previns, PSTR cmdline, int cmdshow) {
	WNDCLASS wndclass;
	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = ins;
	wndclass.hIcon = LoadIcon(0, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(0, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	static TCHAR appname[] = TEXT("HelloWin");
	wndclass.lpszClassName = appname;

	if (!RegisterClass(&wndclass)) {
		MessageBox(NULL, TEXT("This program requries WindowsNT"), appname, MB_ICONERROR);
		return 0;
	}

	HWND wnd = CreateWindow(appname, TEXT("The Hello Program"),
			WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,	CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
			NULL, NULL, ins,NULL);

	ShowWindow(wnd, cmdshow);
	UpdateWindow(wnd);
	
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0)) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}